import 'dart:async';

import 'package:changxindianzishangwu/util/app_color.dart';
import 'package:changxindianzishangwu/util/colors.dart';
import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:r_calendar/r_calendar.dart';

class CalendarDialog extends Dialog {
  BuildContext _context;
  RCalendarController controller;

  CalendarDialog(RCalendarController controller) {
    this.controller = controller;
    // DateTime data =
    //     ObjectUtil.isEmptyString(time) ? DateTime.now() : DateTime.parse(time);
    // print('时间转换；$data');
  }

  @override
  Widget build(BuildContext context) {
    _context = context;
    // TODO: implement build
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
          margin: EdgeInsets.all(15),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(15.0)),
          ),
          child: RCalendarWidget(
            controller: controller,
            customWidget: MyRCalendarCustomWidget(),
            firstDate: DateTime(1970, 1, 1), //当前日历的最小日期
            lastDate: DateTime(2055, 12, 31), //当前日历的最大日期
          ),
        ),
      ),
    );
  }

  void dismiss() {
    Navigator.pop(_context);
  }
}

class MyRCalendarCustomWidget extends RCalendarCustomWidget {
  @override
  Widget buildDateTime(
      BuildContext context, DateTime time, List<RCalendarType> types) {
    TextStyle childStyle;
    BoxDecoration decoration;

    if (types.contains(RCalendarType.disable) ||
        types.contains(RCalendarType.differentMonth)) {
      childStyle = TextStyle(
        color: Colors.grey[400],
        fontSize: 16,
      );
      decoration = BoxDecoration();
    }
    if (types.contains(RCalendarType.normal)) {
      childStyle = TextStyle(
        color: AppColor.colorText85,
        fontSize: 16,
      );
      decoration = BoxDecoration();
    }

    if (types.contains(RCalendarType.today)) {
      childStyle = TextStyle(
        color: AppColor.colorTheme,
        fontSize: 16,
      );
    }

    if (types.contains(RCalendarType.selected)) {
      childStyle = TextStyle(
        color: Colors.white,
        fontSize: 16,
      );
      decoration = BoxDecoration(
        shape: BoxShape.circle,
        color: AppColor.colorTheme,
      );
    }
    return Tooltip(
      message: MaterialLocalizations.of(context).formatFullDate(time),
      child: Container(
          decoration: decoration,
          alignment: Alignment.center,
          child: Text(
            time.day.toString(),
            style: childStyle,
          )),
    );
  }

  List<String> _weeks=["周日","周一","周二","周三","周四","周五","周六"];

  @override
  List<Widget> buildWeekListWidget(
      BuildContext context, MaterialLocalizations localizations) {
    return _weeks
        .map(
          (d) => Expanded(
            child: ExcludeSemantics(
              child: Container(
                height: 60,
                alignment: Alignment.center,
                child: Text(
                  d,
                  style: TextStyle(
                      color: Colors.black,
                      fontSize: 16,
                      fontWeight: FontWeight.w600),
                ),
              ),
            ),
          ),
        )
        .toList();
  }

  @override
  double get childHeight => 50;

  @override
  FutureOr<bool> clickInterceptor(BuildContext context, DateTime dateTime) {
    return false;
  }

  @override
  bool isUnable(BuildContext context, DateTime time, bool isSameMonth) {
    return isSameMonth;
  }

  @override
  Widget buildTopWidget(BuildContext context, RCalendarController controller) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        IconButton(
          icon: Icon(Icons.chevron_left),
          onPressed: () {
            controller.previousPage();
          },
        ),
        SizedBox(
          width: 16,
        ),
        Text(
          DateUtil.formatDate(controller.displayedMonthDate,format: DateFormats.y_mo),
          style: TextStyle(color:AppColor.colorTheme, fontSize: 18),
        ),
        SizedBox(
          width: 16,
        ),
        IconButton(
          icon: Icon(Icons.chevron_right),
          onPressed: () {
            controller.nextPage();
          },
        ),
      ],
    );
  }
}
